//
//  ProductDetailView.m
//  iFlower
//
//  Created by khanh nguyen on 7/23/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "ProductDetailView.h"
#import "Product_Basket_Total.h"
#import "Product.h"
#import "Request.h"
#import "iFlowerAppDelegate.h"
#import "Basket.h"
#import "iFlowerAppDelegate.h"
#import "MyAccountView.h"
#import "LoginView.h"
#import "RegisterView.h"
#import "FullImageView.h"

@implementation ProductDetailView

@synthesize product = _product;
@synthesize titleText = _titleText;
@synthesize lblTitle = _lblTitle;
@synthesize lblProduct = _lblProduct;
@synthesize lblSKU = _lblSKU;
@synthesize lblDesc = _lblDesc;
@synthesize lblPrice = _lblPrice;
@synthesize productImage = _productImage;
@synthesize btnRegister;

-(IBAction)buttonBack:(id)sender
{
	[self.navigationController popViewControllerAnimated:YES];
}
-(IBAction)buttonBuyNow_AddtoBasket:(id)sender
{
	[[[iFlowerAppDelegate shareDelegate] basket] addToBasket:_product];
	Product_Basket_Total * total = [[Product_Basket_Total alloc]initWithNibName:@"Product_Basket_Total" bundle:nil];
	total.titleText = _titleText;
	[self.navigationController pushViewController:total animated:YES];
	[total release];
}
-(IBAction)buutonMenu:(id)sender
{
	[self.navigationController popToRootViewControllerAnimated:YES];
}
-(IBAction)buttonMyAccount:(id)sender
{
	if ([[iFlowerAppDelegate shareDelegate] logined]) {
		MyAccountView * account = [[MyAccountView alloc] initWithNibName:@"MyAccountView" bundle:nil];
		[self.navigationController pushViewController:account animated:YES];
		[account release];
	}
	else {
		LoginView * login = [[LoginView alloc] initWithNibName:@"LoginView" bundle:nil];
		[self.navigationController pushViewController:login animated:YES];
		[login release];		
	}
	
}
-(IBAction) buttonRegister:(id)sender
{
	RegisterView * registerView = [[RegisterView alloc] initWithNibName:@"RegisterView" bundle:nil];
	[self.navigationController pushViewController:registerView animated:YES];
	[registerView release];
}
-(IBAction)fullScreenView
{
	FullImageView * fullImage = [[FullImageView alloc] initWithNibName:@"FullImageView" bundle:nil];
	fullImage.image = _productImage.image;
	[self.navigationController pushViewController:fullImage animated:YES];
	[fullImage release];
}


- (void) contentLoaded:(NSNotification*) notification
{
	NSLog(@"content loaded");
	[[NSNotificationCenter defaultCenter] removeObserver:self];
	NSDictionary* dictionary = [[[Request sharedRequest] entries] objectAtIndex:0];
	_productImage.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[dictionary objectForKey:@"image_url"]]]];
    iFlowerAppDelegate *appDelegate = [iFlowerAppDelegate shareDelegate];
	[appDelegate hideLoadingView];
}

- (void) contentFailed:(NSNotification*) notification
{
	NSLog(@"content failed");
	[[NSNotificationCenter defaultCenter] removeObserver:self];
    iFlowerAppDelegate *appDelegate = [iFlowerAppDelegate shareDelegate];
	[appDelegate hideLoadingView];
}
#pragma mark -
#pragma mark View lifecycle
- (void) viewWillAppear:(BOOL)animated
{
	[super viewWillAppear:animated];
	_lblPrice.text = [NSString stringWithFormat:@"%3.2f", [[[iFlowerAppDelegate shareDelegate] basket] getBasketPrice]];
	if([[iFlowerAppDelegate shareDelegate] logined])
	{
		self.btnRegister.enabled = NO;
	}else {
		self.btnRegister.enabled = YES;
	}
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	_lblTitle.text = _titleText;
	_lblProduct.text = _product.product;
	//_lblPrice.text = _product.price;
	if([_product.fulldesc class]!=[NSNull class])
	_lblDesc.text = _product.fulldesc;
	_lblSKU.text = _product.productCode;
    [[iFlowerAppDelegate shareDelegate] showLoadingView];
	
	[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(contentLoaded:) name: @"ContentLoaded" object: nil];
	[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(contentFailed:) name: @"ContentFailed" object: nil];
	
	[[Request sharedRequest] getProductInfo:_product.productId];
}



/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
    self.btnRegister = nil;
}


- (void)dealloc {
	[btnRegister release];
	[_titleText release];
	[_lblTitle release];
    [super dealloc];
}


@end
